package de.windeln.marketing.campaign.services;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Strings;

import de.windeln.marketing.campaign.costinterval.model.CostintervalClientObject;
import de.windeln.marketing.campaign.dao.interfaces.CampaignMapper;
import de.windeln.marketing.campaign.services.interfaces.CampaignService;
import de.windeln.marketing.campaign.model.RequestClientObject;

public class DefaultCampaignService implements CampaignService{
	
	private CampaignMapper campaignMapper;
	
	@Override
	public List<Map<String, Object>> fetchAllCampaigns() {
		return campaignMapper.fetchCampaigns();
	}
	
	@Override
	public List<Map<String, Object>> fetchAllPromocodes(){
		return campaignMapper.fetchPromocodes();
	}
	
	@Override
	public List<Map<String, Object>> fetchAllCampaignsAndPromocodes(){
		return campaignMapper.fetchAllCampaignsAndPromocodes();
	}
	
	@Override
	@Transactional
	public void insertNewCampaignAndPromocode(RequestClientObject campaign){	
		campaignMapper.insertNewCampaign(campaign);
		if(!Strings.isNullOrEmpty(campaign.getPromocode())){
			campaignMapper.insertNewPromocode(campaign);
		}
		
	}

	@Override
	@Transactional
	public void updateCampaignAndPromocode(RequestClientObject campaign) {
		campaignMapper.updateCampaign(campaign);
		campaignMapper.updatePromocode(campaign);	
	}
	

	@Override
	@Transactional
	public void deleteCampaignAndPromocode(long campaignId) {
		campaignMapper.updateCampaignToDeleteState(campaignId);
		campaignMapper.updatePromocodeToDeleteStateByCampaignId(campaignId);
	}

	@Override
	@Transactional
	public void editCampaignAndPromocode(RequestClientObject campaign) {
//		clientValidator .validate(null, );
		deleteCampaignAndPromocode(campaign.getId());
		insertNewCampaignAndPromocode(campaign);
		
	}
	
	@Required
	public void setCampaignMapper(CampaignMapper campaignMapper) {
		this.campaignMapper = campaignMapper;
	}

	@Override
	@Transactional
	public void updateCampaignAfterInsertingCostInterval(CostintervalClientObject costInterval) {
		campaignMapper.updateCampaignAfterInsertingCostInterval(costInterval);
		
	}

	@Override
	@Transactional
	public void updateCampaignAfterDeletingCostInterval(Long costintervalId) {
		campaignMapper.updateCampaignAfterDeletingCostInterval(costintervalId);
		
	}

	@Override
	@Transactional
	public String findAlreadyExistingCampaign(RequestClientObject rco) {
		return campaignMapper.findAlreadyExistingCampaign(rco);
		
	}

	
}
